define(["jquery", "easy-admin", "echarts", "echarts-theme", "miniAdmin", "miniTab"], function ($, ea, echarts, undefined, miniAdmin, miniTab) {

    var Controller = {
        index: function () {
            get_contrast_data(start_time+' - '+end_time)
            get_class_data(class_time)
            // get_class_count()
            get_class_year(year)
            //第一幅图
            $('#select_type').change(function(){
                var class_time = $('#account_class_year').val();
                var type = $('#accounts_select_class option:selected').val();
                var type2 = $('#accounts_er_select_class option:selected').val();
                if(type2 && type2 !=0){
                    get_class_year(class_time,type2,1,$(this).val())
                }else{
                    get_class_year(class_time,type,0,$(this).val())
                }
            })
           
            //第三幅图的类型选择
            $('#j_select_type').change(function(){
                var start_time = $('#j_start_time').val();
                var end_time = $('#j_end_time').val();
                var type = $('#accounts_s_select_class option:selected').val();
                var type2 = $('#accounts_s_er_select_class option:selected').val();
                if(type2 && type2 !=0){
                    get_contrast_data(start_time+' - '+end_time,type2,1,$(this).val())
                }else{
                    get_contrast_data(start_time+' - '+end_time,type,0,$(this).val())
                }
            })

           

            //第三幅图一级分类
            $('#accounts_s_select_class').change(function(){
                if($(this).val() == 0){
                    $('#s_erji').hide();
                }else{
                    $('#accounts_s_er_select_class').html('');
                    ea.request.get({
                        url: 'final_accounts.echart/getClassEr?&type='+$(this).val(),
                        prefix: true,
                    }, function (res) {
                    },function (res){
                        if(res.code == 200){
                            $('#s_erji').show();
                            var str = '<option value="0">全部</option>'
                            for(var i=0;i<res.data.length;i++){
                                str +='<option value='+res.data[i].id+'>'+res.data[i].title+'</option>';
                            }
                            $('#accounts_s_er_select_class').html(str);
                        }else{
                            ea.msg.error(res.msg)
                        }
                    },'',false);
                }
                var start_time = $('#j_start_time').val();
                var end_time = $('#j_end_time').val();
                get_contrast_data(start_time+' - '+end_time,$(this).val(),0)
 
            })
            //第三幅图二级分类
            $('#accounts_s_er_select_class').change(function(){
                if($(this).val() == 0){
                    var type = $('#accounts_s_select_class option:selected').val();
                    var s = 0
                }else{
                    var type = $(this).val()
                    var s = 1
                }
                var start_time = $('#j_start_time').val();
                var end_time = $('#j_end_time').val();
                get_contrast_data(start_time+' - '+end_time,type,s)
            })
            //第二幅图一级分类
            $('#accounts_total_select_class').change(function(){
                if($(this).val() == 0){
                    $('#di_erji').hide();
                }else{
                    $('#accounts_er_total_select_class').html('');
                    ea.request.get({
                        url: 'final_accounts.echart/getClassEr?&type='+$(this).val(),
                        prefix: true,
                    }, function (res) {
                    },function (res){
                        if(res.code == 200){
                            $('#di_erji').show();
                            var str = '<option value="0">全部</option>'
                            for(var i=0;i<res.data.length;i++){
                                str +='<option value='+res.data[i].id+'>'+res.data[i].title+'</option>';
                            }
                            $('#accounts_er_total_select_class').html(str);
                        }else{
                            ea.msg.error(res.msg)
                        }
                    },'',false);
                }
                var class_time = $('#class_time').val();
                get_class_data(class_time,$(this).val(),0)
            })
            //第二幅图二级分类
            $('#accounts_er_total_select_class').change(function(){
                if($(this).val() == 0){
                    var type = $('#accounts_total_select_class option:selected').val();
                    var s = 0
                }else{
                    var type = $(this).val()
                    var s = 1
                }
                var class_time = $('#class_time').val();
                get_class_data(class_time,type,s)
            })

            //第一幅图一级分类
            $('#accounts_select_class').change(function(){
                if($(this).val() == 0){
                    $('#erji').hide();
                }else{
                    $('#accounts_er_select_class').html('');
                    ea.request.get({
                        url: 'final_accounts.echart/getClassEr?&type='+$(this).val(),
                        prefix: true,
                    }, function (res) {
                    },function (res){
                        if(res.code == 200){
                            $('#erji').show();
                            var str = '<option value="0">全部</option>'
                            for(var i=0;i<res.data.length;i++){
                                str +='<option value='+res.data[i].id+'>'+res.data[i].title+'</option>';
                            }
                            $('#accounts_er_select_class').html(str);
                        }else{
                            ea.msg.error(res.msg)
                        }
                    },'',false);
                }
                var status_type = $('#select_type option:selected').val();
                var class_time = $('#account_class_year').val();
                get_class_year(class_time,$(this).val(),0,status_type)
            })
            //第一幅图二级分类
            $('#accounts_er_select_class').change(function(){
                if($(this).val() == 0){
                    var type = $('#accounts_select_class option:selected').val();
                    var s = 0
                }else{
                    var type = $(this).val()
                    var s = 1
                }
                var status_type = $('#select_type option:selected').val();
                var class_time = $('#account_class_year').val();
                get_class_year(class_time,type,s,status_type)
            })


            //第一幅图时间
            var laydate = layui.laydate;
            laydate.render({
                elem: '#account_class_year'
                ,type: 'year'
                ,trigger: 'click'
                ,value: year
                ,done:function(value){
                    var type = $('#accounts_select_class option:selected').val();
                    var type2 = $('#accounts_er_select_class option:selected').val();
                    var status_type = $('#select_type option:selected').val();
                    if(type2 && type2 !=0){
                        get_class_year(value,type2,1,status_type)
                    }else{
                        get_class_year(value,type,0,status_type)
                    }
                    
                }
            });
            //第二幅图时间
            laydate.render({
                elem: '#class_time'
                ,type: 'month'
                ,trigger: 'click'
                ,range: true
                ,value: class_time
                ,done:function(value){
                    var type = $('#accounts_total_select_class option:selected').val();
                    var type2 = $('#accounts_er_total_select_class option:selected').val();
                    if(type2 && type2 !=0){
                        get_class_data(value,type2,1)
                    }else{
                        get_class_data(value,type,0)
                    }
                }
            });
            //第三幅图时间
            laydate.render({
                elem: '#j_start_time'
                ,type: 'month'
                ,trigger: 'click'
                ,range: true
                ,value: start_time
                ,done:function(value){
                    var type = $('#accounts_s_select_class option:selected').val();
                    var type2 = $('#accounts_s_er_select_class option:selected').val();
                    var status_type = $('#j_select_type option:selected').val();
                    var end_time = $('#j_end_time').val();
                    if(type2 && type2 !=0){
                        get_contrast_data(value+' - '+end_time,type2,1,status_type);
                    }else{
                        get_contrast_data(value+' - '+end_time,type,0,status_type);
                    }
                }
            });
            laydate.render({
                elem: '#j_end_time'
                ,type: 'month'
                ,trigger: 'click'
                ,range: true
                ,value: end_time
                ,done:function(value){
                    var type = $('#accounts_s_select_class option:selected').val();
                    var type2 = $('#accounts_s_er_select_class option:selected').val();
                    var status_type = $('#j_select_type option:selected').val();
                    var start_time = $('#j_start_time').val();
                    if(type2 && type2 !=0){
                        get_contrast_data(start_time+' - '+value,type2,1,status_type);
                    }else{
                        get_contrast_data(start_time+' - '+value,type,0,status_type);
                    }
                   
                }
            });

            miniTab.listen();

        },
        editAdmin: function () {
            ea.listen();
        },
        editPassword: function () {
            ea.listen();
        },
    };

    /**
     * 第一幅图
     */
    function get_class_year(val,type=0,status=0,status_class=1){
        ea.request.get({
            url: 'final_accounts.echart/getClassYear?time='+val+'&type='+type+'&status='+status+'&status_class='+status_class,
            prefix: true,
        }, function (res) {
        },function (res){
            if(res.code == 200){
                echarts_year(res.data)
            }else{
                ea.msg.error(res.msg)
            }
        },'',false);
    }

    /**
     * 第二幅图
     */
    function get_class_data(val,type=0,status=0){
        ea.request.get({
            url: 'final_accounts.echart/getClassData?time='+val+'&type='+type+'&status='+status,
            prefix: true,
        }, function (res) {
        },function (res){
            if(res.code == 200){
                // console.log(res.data)
                echarts_class(res.data)
            }else{
                ea.msg.error(res.msg)
            }
        },'',false);
    }


     /**
     * 第三幅图
     */
      function get_contrast_data(val,type=0,status=0,status_class=1){

        ea.request.get({
            url: 'final_accounts.echart/getContrastData?time='+val+'&type='+type+'&status='+status+'&status_class='+status_class,
            prefix: true,
        }, function (res) {

        },function (res){
            if(res.code == 200){
                echarts_cross(res.data)
            }else{
                ea.msg.error(res.msg)
            }
        },'',false);
    }
    


    /**
     * 年度分类报表
     */
    function echarts_year(data){
        var type = $('#select_class_year option:selected').text();
        if(type == '全部'){
            type = '所有分类';
        }
        var class_time = $('#account_class_year').val();
        var echartsRecords_class_count = echarts.init(document.getElementById('echarts-year'), 'walden');
        if(data.length == 0){
            var class_year_option = {
                title : {
                    text:'暂无数据',
                    x:'center',
                    y:'center',
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: ['01月','02月','03月','04月','05月','06月','07月','08月','09月','10月','11月','12月']
                },
                yAxis: {
                    type: 'value',
                    min: 100,
                    max: 200
                },
            }
        }else{
            var class_year_option = {
                title: {
                    text:class_time+'年'+type+'每月统计',
                    // x:'center',
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                toolbox: {
                    feature: {
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar']},
                        restore: {show: true},
                        saveAsImage: {}
                    }
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: true,
                    scale:true,
                    data: data.name,
                    splitLine:{
                        show:false
                    },
                    axisTick: {
                        alignWithLabel: true
                    }

                },
                yAxis: {
                    type: 'value',
                    splitLine:{
                        show:false
                    },
                },
                series: [
                    {
                        smooth: true,
                        data: data.val,
                        type: 'bar',
                        label:{
                            show:true,
                            position:'top',
                        }
                    }
                ]
            };
        }
        echartsRecords_class_count.setOption(class_year_option);
        window.addEventListener("resize", function () {
            echartsRecords_class_count.resize();
        });
    }


    /**
     * 环比费用报表
     */
    function echarts_cross(data){
        var echartsRecords = echarts.init(document.getElementById('echarts-cross'), 'walden');
        var colors = ['#5470C6', '#EE6666'];
        if(data.length == 0){
            var option = {
                title : {
                    text:'暂无数据',
                    x:'center',
                    y:'center',
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: ['分类','分类','分类','分类']
                },
                yAxis: {
                    type: 'value',
                    min: 100,
                    max: 200
                },
            }
        }else{
            var series_data = [];
            for(var i=0;i<data.series_arr.length;i++){
                if(i == 0){
                    var s = {
                        name: data.series_arr[i].name,
                        type: 'line',
                        xAxisIndex: 1,
                        smooth: true,
                        emphasis: {
                            focus: 'series'
                        },
                        data: data.series_arr[i].data,
                        label:{
                            show:true,
                        }
                    }
                }else{
                    var s = {
                        name: data.series_arr[i].name,
                        type: 'line',
                        smooth: true,
                        emphasis: {
                            focus: 'series'
                        },
                        data: data.series_arr[i].data,
                        label:{
                            show:true,
                        }
                    }
                }
                series_data.push(s)
            }
            var option = {
                color: colors,
                tooltip: {
                    trigger: 'none',
                    axisPointer: {
                        type: 'cross'
                    }
                },
                toolbox: {
                    feature: {
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar']},
                        restore: {show: true},
                        saveAsImage: {}
                    }
                },
                legend: {
                    data:data.time
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        axisTick: {
                            alignWithLabel: true
                        },
                        splitLine:{
                            show:false
                        },
                        axisLine: {
                            onZero: false,
                            lineStyle: {
                                color: colors[1]
                            }
                        },
                        axisPointer: {
                            label: {
                                formatter: function (params) {
                                    return params.value
                                        + (params.seriesData.length ? '：' + params.seriesData[0].data : '');
                                }
                            }
                        },
                        data: data.name,
                        axisLabel: { interval: 0, rotate: 30 }
                    },
                    {
                        type: 'category',
                        axisTick: {
                            alignWithLabel: true
                        },
                        splitLine:{
                            show:false
                        },
                        axisLine: {
                            onZero: false,
                            lineStyle: {
                                color: colors[0]
                            }
                        },
                        axisPointer: {
                            label: {
                                formatter: function (params) {
                                    return params.value
                                        + (params.seriesData.length ? '：' + params.seriesData[0].data : '');
                                }
                            }
                        },
                        data: data.name,
                        axisLabel: { interval: 0, rotate: 30 }
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        splitLine:{
                            show:false
                        },
                    }
                ],
                series: series_data
            };
        }

        echartsRecords.setOption(option);
        window.addEventListener("resize", function () {
            echartsRecords.resize();
        });
    }


    /**
     * 分类总费用报表
     */
    function echarts_class(data){
        var echartsRecords = echarts.init(document.getElementById('echarts-records'), 'walden');
        if(data.length==0){
            var optionRecords = {
                title : {
                    text:'暂无数据',
                    x:'center',
                    y:'center',
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: ['分类','分类','分类','分类']
                },
                yAxis: {
                    type: 'value',
                    min: 100,
                    max: 200
                },
            }
        }else{
            var series = [];
            for (var key in data.series) {
                var s = {
                    name: key,
                    type: 'line',
                    data: data.series[key],
                    label:{
                        show:true,
                    }
                }
                series.push(s)
            }

            var optionRecords = {
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: data.keys
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                toolbox: {
                    feature: {
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar']},
                        restore: {show: true},
                        saveAsImage: {}
                    }
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: data.name,
                    splitLine:{
                        show:false
                    },
                    axisLabel: { interval: 0, rotate: 30 }
                },
                yAxis: {
                    type: 'value',
                    splitLine:{
                        show:false
                    },
                },
                series: series
            };
        }

        echartsRecords.setOption(optionRecords);
        window.addEventListener("resize", function () {
            echartsRecords.resize();
        });
    }

    return Controller;
});
